import {_decorator, Component, Prefab} from "cc";
import {FishInPool} from "db://assets/script/game/fish/fish-in-pool";
import {FishStatus} from "../../../proto/shared/v1/biz";

const {ccclass, property} = _decorator;

@ccclass('BattleTuple')
export class BattleTuple extends Component {

    @property(Prefab)
    fishInPoolPrefab!: Prefab

    battleTupleId: string | null = null;
    fishId!: string
    inBattle: boolean = false;

    role: FishInPool = null!;
    enemy: FishInPool = null!;

    init(fishId: string, heal: number, maxHeal: number, status: FishStatus) {
        // this.fishId = fishId
        // this.battleTupleId = uuidv4()
        // const fishNode = instantiate(this.fishInPoolPrefab)
        // fishNode.setPosition(new Vec3(0, 250, 0))
        // this.node.addChild(fishNode)
        // const fishInPool = fishNode.getComponent(FishInPool)
        // this.role = fishInPool
        // fishInPool.init(this.battleTupleId, fishId, heal, maxHeal, true, status)
        // this.onEvent()
    }

    onEvent() {
        // director.on(SocketEvent.battle, this.battle, this)
        // director.on(GameEvent.battleEndPrefix + this.battleTupleId, this.beginEnd, this)
    }

    // battleBegin(enemyId: string, heal: number, maxHeal: number) {
    //     if (this.inBattle) {
    //         return
    //     }
    //     const basePos = this.role.node.worldPosition
    //     const offsetX = -100 + Math.random() * 200
    //     const height = this.node.parent.getComponent(UITransform).height
    //
    //     const fishNode = instantiate(this.fishInPoolPrefab)
    //     this.node.addChild(fishNode)
    //     fishNode.setWorldPosition(new Vec3(basePos.x + offsetX, basePos.y + height - 260))
    //     const fishInPool = fishNode.getComponent(FishInPool)
    //     fishInPool.init(this.battleTupleId, enemyId, heal, maxHeal, false, FishStatus.alive)
    //     this.enemy = fishInPool
    //     this.inBattle = true
    //     fishInPool.walkTo(-offsetX, -800, 0.8)
    // }
    //
    // beginEnd(win: boolean) {
    //     if (!this.inBattle) {
    //         return
    //     }
    //     this.inBattle = false
    //     // todo 打扫战场
    //     if (win) {
    //         this.enemy.destroy()
    //     } else {
    //         this.role.destroy()
    //     }
    //
    // }

    //
    // /**战斗状态 */
    // battle(body: FishAttackRecordReply) {
    //     if (body.attack_id != this.role.fishId && body.defence_id != this.role.fishId) {
    //         director.emit(GameEvent.beAttack, body);
    //         director.emit(GameEvent.attack, body);
    //         return;
    //     }
    //     let roleIsAttacker = this.role.fishId == body.attack_id;
    //     let animeIsAttacker = this.role.fishId == body.defence_id;
    //     if (!roleIsAttacker && !animeIsAttacker) {
    //         director.emit(GameEvent.beAttack, body);
    //         director.emit(GameEvent.attack, body);
    //         return;
    //     }
    //     const rdTs = Math.random() * 0.5
    //     if (roleIsAttacker) {
    //         if (!this.inBattle) {
    //             this.battleBegin(body.defence_id, body.defence_heal as number, body.defence_max_heal as number)
    //         } else {
    //             this.scheduleOnce(() => {
    //                 this.role.fireBullet(body)
    //             }, rdTs)
    //         }
    //     } else {
    //         if (!this.inBattle) {
    //             this.battleBegin(body.attack_id, body.attack_heal as number, body.attack_max_heal as number)
    //         } else {
    //             this.scheduleOnce(() => {
    //                 this.enemy.fireBullet(body)
    //             }, rdTs)
    //         }
    //     }
    // }


}